package com.UKIT.airtickets

import TicketAdapter
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
import com.UKIT.airtickets.Service.Handlers.FavouritesHandler
import com.UKIT.airtickets.Service.Tikets
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.mockito.Mockito.*

class FavouritesHandlerTest {

    @Test
    fun updateFavoritesList_Success() {
        // Arrange
        val adapter = mock(TicketAdapter::class.java)
        val total = mock(TextView::class.java)
        val favouritesHandler = FavouritesHandler(ApplicationProvider.getApplicationContext())

        val ticketsList = listOf(
            Tikets(
                searchToken = "token1",
                startCity = "City1",
                startCityCode = "C1",
                endCity = "City2",
                endCityCode = "C2",
                startDate = "2024-05-10",
                endDate = "2024-05-11",
                price = 100
            ),
            Tikets(
                searchToken = "token2",
                startCity = "City3",
                startCityCode = "C3",
                endCity = "City4",
                endCityCode = "C4",
                startDate = "2024-05-12",
                endDate = "2024-05-13",
                price = 200
            )
        )

        // Stubbing getFavoritesFromDatabase method to return test data
        runBlocking {
            `when`(favouritesHandler.getFavoritesFromDatabase()).thenReturn(ticketsList)
        }

        // Act
        runBlocking { favouritesHandler.updateFavoritesList(adapter, total) }

        // Assert
        verify(adapter).setData(ticketsList)
        verify(total).text = StringBuilder("Сумма: 300") // Total sum of prices in test data
    }
}